'use client';

import type { ISectionGroup } from '@/interfaces';
import { queryAllSectionGroup } from '@/services/api';
import { useQuery } from '@tanstack/react-query';
import classNames from 'classnames';
import Alert from '@/app/[locale]/alert/alert';
import AlertLoad from '@/app/[locale]/alert/load';
import { SectionGroupAdminContext } from '@/contexts/admin/forum/sectionGroup';
import { useContext, useState } from 'react';
import CreateSectionGroupAdminPage from '@/app/[locale]/admin/forum/sectionGroup/create';
import DetailsSectionGroupAdminPage from '@/app/[locale]/admin/forum/sectionGroup/detail';
import UpdateSectionGroupAdminPage from '@/app/[locale]/admin/forum/sectionGroup/update';

export default function ListSectionGroupAdminPage(this: any) {
  const queryKey = ['/forum', '/sectionGroups'];
  const context = useContext(SectionGroupAdminContext);
  const [isCreate, setIsCreate] = useState(false);
  const [detailItem, setDetailItem] = useState<ISectionGroup>();
  const [updateItem, setUpdateItem] = useState<ISectionGroup>();
  const [currentItemId, setCurrentItemId] = useState<number>();

  const queryAllSectionGroupQuery = useQuery(
    queryKey,
    async () => {
      return (await queryAllSectionGroup()) as ISectionGroup[];
    },
    {
      keepPreviousData: true,
    },
  );

  function onClickCreate() {
    setIsCreate(true);
  }

  function onClickDetail(item: ISectionGroup) {
    setDetailItem(item);
    setCurrentItemId(item.id);
  }

  function onClickUpdate(item: ISectionGroup) {
    setUpdateItem(item);
  }

  if (queryAllSectionGroupQuery.data) {
    const data = queryAllSectionGroupQuery.data;

    return (
      <SectionGroupAdminContext.Provider
        value={{
          ...context,
          isCreate,
          detailItem,
          updateItem,
          setIsCreate,
          setDetailItem,
          setUpdateItem,
          queryKey,
        }}
      >
        <div className="row">
          <div className="col">
            <div className="vstack gap-4">
              {isCreate ? (
                <CreateSectionGroupAdminPage />
              ) : (
                <div className="row">
                  <div className="col">
                    <div className="card">
                      <div className="card-header bg-transparent">
                        <div className="row row-cols-auto justify-content-between g-2">
                          <div className="col"></div>
                          <div className="col">
                            <div className="hstack gap-3">
                              <button
                                onClick={onClickCreate}
                                type="button"
                                className="btn btn-primary"
                              >
                                <i className="bi bi-plus-lg me-2"></i>
                                新增版块组
                              </button>
                            </div>
                          </div>
                        </div>
                      </div>

                      <div className="card-body">
                        <div className="table-responsive">
                          <table className="table table-hover align-middle">
                            <thead>
                              <tr className="text-nowrap">
                                <th scope="col" className="w-10">
                                  ID
                                </th>
                                <th scope="col" className="w-10">
                                  名称
                                </th>
                                <th scope="col" className="w-10">
                                  排序
                                </th>
                                <th scope="col" className="w-10"></th>
                              </tr>
                            </thead>
                            <tbody>
                              {data.map((item) => {
                                return (
                                  <tr
                                    key={item.id}
                                    className={classNames('text-nowrap', {
                                      'table-active': currentItemId === item.id,
                                    })}
                                  >
                                    <td
                                      className="cursor-pointer"
                                      onClick={onClickDetail.bind(this, item)}
                                    >
                                      {item.id}
                                    </td>
                                    <td
                                      className="cursor-pointer"
                                      onClick={onClickDetail.bind(this, item)}
                                    >
                                      {item.name}
                                    </td>
                                    <td
                                      className="cursor-pointer"
                                      onClick={onClickDetail.bind(this, item)}
                                    >
                                      {item.sort}
                                    </td>
                                    <td>
                                      <button
                                        onClick={onClickUpdate.bind(this, item)}
                                        className="btn btn-sm btn-light me-2"
                                        type="button"
                                      >
                                        <i className="bi bi-pencil me-2"></i>
                                        修改
                                      </button>
                                    </td>
                                  </tr>
                                );
                              })}
                            </tbody>
                          </table>

                          {data.length === 0 && <Alert message="暂无版块组" />}
                        </div>
                      </div>
                    </div>
                  </div>
                </div>
              )}

              {detailItem && <DetailsSectionGroupAdminPage />}

              {updateItem && <UpdateSectionGroupAdminPage />}
            </div>
          </div>
        </div>
      </SectionGroupAdminContext.Provider>
    );
  }

  if (queryAllSectionGroupQuery.error) {
    return <Alert message={queryAllSectionGroupQuery.error} type="error" />;
  }

  return <AlertLoad />;
}
